package models

import (
	"gorm.io/gorm"
	"srv/house"
)

type House struct {
	gorm.Model
	UserId        int     `gorm:"type:int;not null;comment:'用户Id'"`
	HouseName     string  `gorm:"type:varchar(30);not null;comment:'房源名称'"`
	HouseCity     string  `gorm:"type:varchar(30);not null;comment:'房源城市'"`
	HouseAdd      string  `gorm:"type:varchar(100);not null;comment:'小区地址'"`
	HouseBuilding string  `gorm:"type:char(10);not null;comment:'楼栋号'"`
	HouseFloor    int     `gorm:"type:int;not null;comment:'楼层'"`
	HouseArea     string  `gorm:"type:char(30);not null;comment:'房源面积'"`
	HousePrice    float64 `gorm:"type:decimal(10,2);default: 2500;comment:'月租金'"`
	HouseInfo     string  `gorm:"type:varchar(100);not null;comment:'房源描述'"`
	Status        int     `gorm:"type:int;default: 1;comment:'状态: 1-待上架 2-以上架 3-以租赁'"`
}

func (h *House) FindHouse(houseName string) error {
	return DB.Debug().Where("house_name = ?", houseName).First(&h).Error
}
func (h *House) FirstHouse(houseId int) error {
	return DB.Debug().Where("id = ?", houseId).First(&h).Error
}
func (h *House) CreateHouse() error {
	return DB.Debug().Create(&h).Error
}
func (h *House) PersonListHouse(UserId int) (houses []*house.PersonList) {
	var hou []House
	DB.Debug().Where("user_id = ?", UserId).Order("created_at desc").Find(&hou)
	for _, h2 := range hou {
		list := house.PersonList{
			HouseName:     h2.HouseName,
			HouseCity:     h2.HouseCity,
			HouseAdd:      h2.HouseAdd,
			HouseBuilding: h2.HouseBuilding,
			HouseFloor:    int64(h2.HouseFloor),
			HouseArea:     h2.HouseArea,
			HousePrice:    float32(h2.HousePrice),
			HouseInfo:     h2.HouseInfo,
			Status:        int64(h2.Status),
		}
		houses = append(houses, &list)
	}
	return
}
func (h *House) ListHouse() (houses []*house.HouseList) {
	var hou []House
	DB.Debug().Find(&hou)
	for _, h2 := range hou {
		list := house.HouseList{
			UserId:        int64(h2.UserId),
			HouseName:     h2.HouseName,
			HouseCity:     h2.HouseCity,
			HouseAdd:      h2.HouseAdd,
			HouseBuilding: h2.HouseBuilding,
			HouseFloor:    int64(h2.HouseFloor),
			HouseArea:     h2.HouseArea,
			HousePrice:    float32(h2.HousePrice),
			HouseInfo:     h2.HouseInfo,
			Status:        int64(h2.Status),
		}
		houses = append(houses, &list)
	}
	return
}
func (h *House) StatusHouse(houseId int) error {
	return DB.Debug().Where("id = ?", houseId).Updates(&h).Error
}

type HouseImage struct {
	gorm.Model
	UserId    int    `gorm:"type:int;not null;comment:'用户Id'"`
	HouseId   int    `gorm:"type:int;not null;comment:'房源Id'"`
	ImageName string `gorm:"type:varchar(30);not null;comment:'图片名称'"`
	Image     string `gorm:"type:varchar(255);not null;comment:'房源图片'"`
}

func (h *HouseImage) CreateHouseImage() error {
	return DB.Debug().Create(&h).Error
}
